package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.Collections;

public class RestClient {

    private static final String URL = "https://wechat.wecity.qq.com/trpcapi/Diagnosis_GuidePlatformServer_AIGuideGeneral/predict";
    private static final String TH_SESSION_ID = "nszli17119347625400GvAssLfdeBgqLthu-xedJFSrLE04f75360";
    private static final String SUB_BUSINESS_ID = "guide-h5";
    private static final String BUSINESS_ID = "tencent-health-h5";
    private static final String REQUEST_BODY = "{\n" +
            "    \"request\": {\n" +
            "        \"req\": {\n" +
            "            \"appid\": \"93bf4050-113b-11ec-a8ae-a3e6aa8cbf00\",\n" +
            "            \"message\": \"头晕\",\n" +
            "            \"appidType\": 3,\n" +
            "            \"versionType\": 1,\n" +
            "            \"sex\": 1,\n" +
            "            \"age\": 28,\n" +
            "            \"pHospitalId\": \"\",\n" +
            "            \"ecardNo\": \"\",\n" +
            "            \"platformId\": \"1\",\n" +
            "            \"sessionId\": \"\"\n" +
            "        },\n" +
            "        \"header\": {\n" +
            "            \"requestId\": \"d3bc264a-96d1-4f37-a714-274f5a667d09\"\n" +
            "        }\n" +
            "    },\n" +
            "    \"service\": \"Diagnosis_GuidePlatformServer_AIGuideGeneral\",\n" +
            "    \"func\": \"predict\",\n" +
            "    \"context\": {}\n" +
            "}";

    public static void main(String[] args) {
        callPredictEndpoint();
    }

    public static void callPredictEndpoint() {
        RestTemplate restTemplate = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("Th-Session-Id", TH_SESSION_ID);
        headers.set("Th-Sub-Business-Id", SUB_BUSINESS_ID);
        headers.set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x6309092b) XWEB/9079");
        headers.setAccept(Collections.singletonList(MediaType.ALL));
        headers.set("Origin", "https://miying.qq.com");
        headers.set("Sec-Fetch-Site", "same-site");
        headers.set("Sec-Fetch-Mode", "cors");
        headers.set("Sec-Fetch-Dest", "empty");
        headers.set("Referer", "https://miying.qq.com/");
        headers.set("Accept-Language", "zh-CN,zh;q=0.9");

        HttpEntity<String> entity = new HttpEntity<>(REQUEST_BODY, headers);

        ResponseEntity<JSONObject> response = restTemplate.postForEntity(URL, entity, JSONObject.class);

        System.out.println(JSON.toJSONString(response.getBody(), true));

        // 进行响应处理...
    }
}

// 在实际使用中，对响应体进行处理
// 示例：
// if (response.getStatusCode().is2xxSuccessful()) {
//     String responseBody = response.getBody();
//     // 解析并处理响应 JSON 数据
// } else {
//     // 处理非成功状态码
// }
